package com.sdau.zhang.service.impl;

import com.sdau.zhang.dao.LuntanDao;
import com.sdau.zhang.service.ILuntanService;
import com.sdau.zhang.vo.Luntan;
import com.sdau.zhang.vo.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//论坛service
@Service
public class LuntanServiceImpl implements ILuntanService {
    @Autowired
    private LuntanDao luntanDao;

    @Override
    @Transactional
    public boolean insert(Luntan luntan) {

        int effectNum = luntanDao.insertLuntan(luntan);
        if(effectNum>0)
            return true;
        else
            return  false;
    }

    @Override
    public List<Luntan> getByTid(int tid,int begin,int limitCount) {
        return luntanDao.getByTid(tid,begin,limitCount);
    }

    @Override
    public void deleteLuntan(int tid, int fid) {
        luntanDao.deleteLuntan(tid,fid);
    }

    @Override
    public String getTopic(int tid) {
        return luntanDao.getTopic(tid);
    }

    @Override
    public Map<Integer, List<Response>> getResponse(int tid,int page) {
        Map<Integer,List<Response>> ml = new HashMap<Integer,List<Response>>();
        List<Response> allResponse = luntanDao.getAllResponse(tid,page);
        for(int i =1;i<=10;i++) {
            int floor = (page-1)*10+i;
            List<Response> lr = new ArrayList<Response>();
            for (Response r : allResponse) {                //遍历大list，然后把相应楼层的回复装载
                if(r.getFloor()==floor)
                    lr.add(r);
            }
            ml.put(floor,lr);                                  //添加进map,key为floor
        }
        return ml;
    }


}
